﻿//----------------------------------------------
// 表单自定义的数据
// Version :V 0.1.0
// ----------------------------------------------------------
var DataTable = function (tid) {
    _this = this;
    _list = {
        verion: "v 0.1.0",
        tableId: "",
        author: "江怀玉",
        Editable: false,
        FExpression: "5=2",
        Entity: "",
        ///列名集合
        Column: [],
        ///行值集合
        Rows: [],
        ///定义当前行
        CureentRow: "",
        SelectedIndex: -1,
        ///获取行
        getRow: function (i) {
            // this.SelectedIndex = i;
            return this.Rows[i];
        },
        FirstHTML: ""
    ,
        ///获取当前实体
        getEntity: function () {
            return $($("#" + tid + " table")[0]).attr("name");
        },
        ///获取列名集合
        getColumn: function () {
            var _Column = [];
            $($("#" + tid + " table tbody")[0]).find("input,select").each(function () {
                _Column.push($(this).attr("name"));
                $(this).val("");
            });
            this.Column = _Column;
        },
        ///获取当前行指定的列值
        getColumnValue: function (o) {
            if (typeof (o) == "String") {
                return eval("this.CureentRow." + o);
            }
            else {
                return eval("this.CureentRow." + Column[o]);
            }
        },
        ///数据
        _DataBind: function () {
            //debugger
            //alert(this.Entity);
            this.Entity = this.getEntity();
            this.getColumn();
            ///this.DataBind(this.Entity);
            this.getDataSource();
        },
        ///获取数据源
        getDataSource: function () {
            //debugger
            //alert(this.FExpression);
            var strUrl = "";
            if (this.FExpression == "" || this.FExpression.split('=')[1] == "''") {
                strUrl = "AjaxData/DataTableJsonlist.aspx?r=" + Math.random() + "&order=RecID&pageindex=1&pagesize=5000&entity=" + this.Entity;
            } else {
                strUrl = "AjaxData/DataTableJsonlist.aspx?r=" + Math.random() + "&order=RecID&condition=" + this.FExpression + "&pageindex=1&pagesize=5000&entity=" + this.Entity;
            }
            //debugger
            this.getColumn();
            var col = this.Column.length;
            alert(col);
            this.FirstHTML = $("#" + this.Entity + " tr")[1].outerHTML;
            $.get(strUrl, function (data, textStatus) {
                //alert(data);
                if (data != "{\"total\":0,\"rows\":}" && data != "[{\"N\":\"null\"}]") {
                    _list.Rows = eval("(" + data + ")").rows;
                } else {
                    // alert(_list.Entity
                    //var o = clone(eval(_list.Entity));
                    //_list.Rows.push(o);
                }
                _list.Render();
                var trs = $("#" + _list.Entity + " tr");
                for (var i = 0; i < _list.Rows.length; i++) {
                    $(trs[i + 1]).JSONToForm(_list.Rows[i]);
                }
                var th = $("<thead><thead>").append($("#" + _list.Entity + " tr:first"));
                $("#" + _list.Entity).prepend(
                th
            );
                $("#" + _list.Entity + " tr:first").remove();
                //debugger
                if (typeof (parent._CancelFlag) != "undefined") {
                    //退回标标/ID/草稿
                    if (parent._CancelFlag == "4" || getPar1("id") == "" || getPar1("statues") == "2") {
                        {
                            $("#" + _list.Entity + " thead:first").prepend(
                        $("<tr><td colspan=\"" + col + "\"><button id='_btnNew' onclick='_list.AddRow();return false;'>新增</button><button id='_btnNew' onclick='_list.DeleteRow();return false;'>删除</button></td></tr>")
                );
                        }
                    }
                }
                else {
                    //退回标标/ID/草稿
                    if (getPar1("id") == "") {
                        $("#" + _list.Entity + " thead:first").prepend(
                    $("<tr><td colspan=\"" + col + "\"><button id='_btnNew' onclick='_list.AddRow();return false;'>新增</button><button id='_btnNew' onclick='_list.DeleteRow();return false;'>删除</button></td></tr>")
                );
                    } else {
                        //非流程
                        //alert(getPar1("entity").indexOf("bpm_"));
                        if (getPar1("entity").indexOf("bpm_") == -1 || getPar1("statues") == "2") {
                            $("#" + _list.Entity + " thead:first").prepend(
                    $("<tr><td colspan=\"" + col + "\"><button id='_btnNew' onclick='_list.AddRow();return false;'>新增</button><button id='_btnNew' onclick='_list.DeleteRow();return false;'>删除</button></td></tr>")
                    );

                        }
                    }
                }

                if (data != "{\"total\":0,\"rows\":}") {
                    fnSelected();
                }
                //$("#" + _list.Entity + " tr:first").remove();
            });

        },
        ///重写控制
        Render: function () {
            //var trs = $($($("#" + this.Entity + " tr")[1]).html());
            $($("#" + this.Entity + " tr")[1]).remove();
            //debugger
            if (this.Rows.length > 0) {
                for (var i = 0; i < this.Rows.length; i++) {
                    var trs = $(this.FirstHTML.replace("data_row", "id" + i));
                    trs.appendTo($($("#" + tid + " table")));
                    //debugger
                    if (typeof (parent._CancelFlag) != "undefined") {
                        //退回标标/ID/草稿
                        if (parent._CancelFlag != "4" && getPar1("id") != "" && getPar1("statues") != "2") {
                            trs.setInputReadOnly(true);
                        }
                    }
                    else {
                        if (getPar1("id") != "") {
                            if (getPar1("entity").indexOf("bpm_") > -1) {
                                trs.setInputReadOnly(true);
                            }
                        }
                    }
                }
                //this.AddRow();
            }
            //初始化
            if (typeof (initControls) == "function") {
                var _time = $("input[type=Time]");
                initControls(_time, "Time");
                var _date = $("input[type=Date]");
                initControls(_date, "Date");
            }
        },
        //增加行
        AddRow: function () {
            //debugger 
            var o = clone(eval(_list.Entity));
            this.Rows.push(o);
            $("#" + tid + " tbody tr").unbind();
            var i = this.Rows.length - 1
            var trs = $(this.FirstHTML.replace("data_row", "id" + this.Rows.length - 1));
            trs.appendTo($($("#" + tid + " table")));
            trs.find("input").bind('blur', function () {
                _list.Modify();
            });
            $(trs).JSONToForm(_list.Rows[i]);
            //fnSelected();
            //初始化
            if (typeof (initControls) == "function") {
                var _time = $("input[type=Time]");
                initControls(_time, "Time");
                var _date = $("input[type=Date]");
                initControls(_date, "Date");

            }
        },
        //删除行
        DeleteRow: function () {
            //debugger
            var i = 0;
            //$("#"+tid+" tbody tr")
            $("#" + tid + " tbody .selected").remove();
            this.Rows.REMOVE(this.CureentRow);
        },
        //修改
        Modify: function () {
            $("#" + tid + " tbody tr.selected").SetFormValueToObject(this.CureentRow);
        },
        //选定某条数据
        Selected: function (e) {
            this.SelectedIndex = 0;
            var trs1 = $("#" + tid + " tbody tr");
            trs1.each(function (em) {
                var _class = $(trs1[em]).attr("class");
                if (_class == undefined) {
                    return;
                }
                if ($(trs1[em]).attr("class").indexOf("selected") > -1) {
                    _list.SelectedIndex = em;
                }
            });
            if (_list.SelectedIndex.toString() == "NaN") {
                _list.SelectedIndex = 0;
            }
            this.CureentRow = this.Rows[this.SelectedIndex];
            //$("#test")[0].innerHTML = (this.CureentRow.ID);
        },
        ///在数据提交前将表格中数据跟数据对象同步
        GetData: function () {
            var trs = $("#" + tid + " tbody tr");
            trs.each(function (em) {
                $("#" + tid + " tbody tr").removeClass("selected");
                $(trs[em]).addClass("selected");
                _list.Selected($(trs[em]));
                _list.Modify();
            });

        }
    }
}
var _d = new _list();
alert(_d.list.verion);
//选择
//var fnSelected = function () {
//    $("#"+tid+" tbody tr").bind('click', function (evt) {
//        _list.Modify();
//        $("#"+tid+" tr").removeClass("selected");
//        $(this).addClass("selected");
//        _list.Selected($(this));
//    });
//}

///// 检查数据
//var fnCheckEmpty = function () {
//    var trs = $("#"+tid+" tbody tr");
//    if (trs.length <= 1) {
//        return true;
//    }
//    trs.each(function (em) {
//        var t = $(trs[em]).find("input");
//        var _t = "";
//        t.each(function (_em) {
//            _t = _t + $(t[_em]).val();
//        });
//        if (_t == "") {
//            return false;
//        }
//    });
//    return true;
//}